/************************************************************************/
/*    Author: Scott Bevin                                               */
/************************************************************************/

#include "OgreRenderer.h"
#include "../../XUtilities/Logger.h"

#include <string>

namespace XF
{
	OgreRenderer::OgreRenderer()
	{
		mRoot = NULL;
		mWindow = NULL;
		mDefaultSceneMgr = NULL;
		mDefaultCamera = NULL;
		mDefaultViewport = NULL;
	}

	OgreRenderer::~OgreRenderer()
	{
	}

	bool OgreRenderer::Initialize(std::string windowTitle, std::string resourceFile)
	{
		if(!CreateRoot()) return false;

		DefineResources(resourceFile);

		if(!CreateRendersystem()) return false;

		CreateRenderWindow(windowTitle);

		CreateDefaultScene();

		return true;
	}

	void OgreRenderer::ShutDown()
	{
		mWindow->removeViewport(mDefaultViewport->getZOrder());
		mDefaultSceneMgr->destroyCamera(mDefaultCamera);
		mRoot->destroySceneManager(mDefaultSceneMgr);
		mWindow->destroy();
		mRoot->shutdown();

		delete mRoot;
		mRoot = NULL;
	}

	bool OgreRenderer::CreateRoot()
	{
		try
		{
			mRoot = new Ogre::Root();
		}
		catch(...)
		{
			return false;
		}

		return (mRoot != NULL);
	}

	void OgreRenderer::DefineResources(std::string resourceFile)
	{
		std::string secName, typeName, archName;

		Ogre::ConfigFile cf;
		cf.load(resourceFile);

		Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

		while(seci.hasMoreElements())
		{
			secName = seci.peekNextKey();

			Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator i;

			for(i = settings->begin(); i != settings->end(); ++i)
			{
				typeName = i->first;
				archName = i->second;

				SSELOG << "<XF::OgreRenderer>: Adding resource location (Typename: " << typeName << ", Archname: " << archName;

				Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
			}
		}
	}

	bool OgreRenderer::CreateRendersystem()
	{
		if(/*!mRoot->restoreConfig() &&*/ 
			!mRoot->showConfigDialog())
			return false;

		return true;
	}

	void OgreRenderer::CreateRenderWindow(std::string windowTitle)
	{
		mWindow = mRoot->initialise(true, windowTitle);
	}

	void OgreRenderer::InitializeAllResources(int mipLevel)
	{
		Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
		Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
	}

	void OgreRenderer::CreateDefaultScene()
	{
		mDefaultSceneMgr = mRoot->createSceneManager(Ogre::ST_EXTERIOR_FAR, "Default SceneManager");
		mDefaultCamera = mDefaultSceneMgr->createCamera("Default Camera");
		//mDefaultCamera->setProjectionType(Ogre::ProjectionType::PT_ORTHOGRAPHIC);
		mDefaultCamera->setNearClipDistance(0.001f);
		mDefaultViewport = mWindow->addViewport(mDefaultCamera);
	}
}
